using GZ.Common.Data;
using Newtonsoft.Json;
using GZ.DB.Repository.GZ_YUENAN_JX_WCS;
using GZ.DB.Entity.GZ_YUENAN_JX_WCS;
using GZ.DB.IRepository.GZ_YUENAN_JX_WCS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Configuration;
using GZ.Projects.WCS_SVC;
using System.Net.NetworkInformation;
using GZ.Common.Code;

namespace GZ.Projects.WCS_SVC
{

	#region IC01.RGV作业任务下发M

	/// <summary>
	/// wms任务下发接口参数
	/// </summary>
	[Serializable]
	public class ToRgvCreateTask
	{
		public string taskNo;
		public int taskType;
		public string loadPoint; //取货点位置
		public string UnloadPoint; //放货点位置
	}
	[Serializable]
	public class FromRgvTaskResponse
	{
		public int code; //0失败1成功
		public bool state;
		public string message;
		public object dataInfo;
	}

	#endregion

	#region IC02.RGV作业任务状态上报给WCS
	/// <summary>
	/// wms取消任务接口参数
	/// </summary>
	[Serializable]
	public class FromRgvTaskStatus
	{
		public string taskNo;
		public int status;
		public string barCode;
		public string message;
	}
	[Serializable]
	public class ToRgvTaskStatusResponse
	{
		public int code;
		public bool state;
		public string message;
		public object dataInfo;
	}
	#endregion

	#region IC03.Rgv状态获取G
	[Serializable]
	public class ToRgvGetInfo
	{
		public string number;
		public string name;
		public string connectState;
		public string deviceMode;
		public string faultStatus;
	}
	[Serializable]
	public class FromRgvInfoResponse
	{
		public string number;
		public string name;
		public string connectState;
		public string deviceMode;
		public string faultStatus;
	}

	#endregion



	public class WCSTORGV
	{
		private static string url = ConfigurationManager.AppSettings["WCSTORGVTASK"]; //url都从配置项获取
		public bool ToRgvTask(ToRgvCreateTask rgvTaskDispatchRequest,out string message)
		{
			string msg = $"\r\n[wcs下发任务信息至rgv：Start{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
			message = null;
			FromRgvTaskResponse response = new FromRgvTaskResponse();
			try
			{

				string input = Json.ToJson(rgvTaskDispatchRequest);
				msg += $"\r\n\t下发json:[{input}]{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
				//string json = new HttpClient().Call(input, url, out errorMsg);
				string json = new HttpClient().Call("POST",input, url, out message);
				msg += $"\r\n\t收到回复json:[{json}]{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
				response = Json.ToObject<FromRgvTaskResponse>(json);
				//rgv接口文档中 0 失败 1成功
				if (response != null && response.code == 1)
				{
					message = json;
					return true;
				}
				else
				{
					message = $"{json},rgv反馈下发失败异常信息：{response.message}";
					return false;
				}
			}
			catch (Exception ex)
			{
				msg += ex.Message;
				return false;
			}
			finally
			{
				msg = msg.Replace('\'', '\"');
				Conn.YNJXLog.Info(1, $"Wcs下发任务至Rgv ", 1, msg);
			}
		}
	}


	public static class ToRgvHandle
	{
		/// <summary>
		/// 任务回复 ToWmsResponseOrderData JSON 文本
		/// </summary>
		private static string BuildResponseToRgv(int code, bool state ,string message = null, object dataInfo = null)
		{
			// 截断 msg 到 256 字符,系统异常返回值太长，需要截断
			if (!string.IsNullOrEmpty(message) && message.Length > 256)
				message = message.Substring(0, 256);
			var resp = new ToRgvTaskStatusResponse
			{
				code = code,
				state =state,
				message = message,
				dataInfo = dataInfo
			};
			return JsonConvert.SerializeObject(resp);
		}
		
		/// <summary>
		/// IC01.Rgv任务状态获取
		/// </summary>
		/// <param name="requestJson"></param>
		/// <returns></returns>
		public static string GetRgvTaskStatusInfo(string requestJson)
		{
			string respJson = string.Empty;
			//1-任务开始，2-取货中，3-取货完成，4-放货中，5-放货完成，6-任务取消，7-异常
			try
			{
				var request = JsonConvert.DeserializeObject<FromRgvTaskStatus>(requestJson);
				IRgvTaskRepository rtr = new RgvTaskRepository();
				RgvTaskEntity rgvTaskET = rtr.FindEntity(t => t.TASKNO == request.taskNo);
				if(rgvTaskET != null)
				{
					AutoScanApp autosan = new AutoScanApp();
					switch(request.status)
					{
						case 1: rgvTaskET.TASKSTATE= "Start";					
								  bool rgv1 = autosan.UpdateRgvTask(rgvTaskET, out string rgvMsg1); //任务开始send改为Start
						break;
						case 2: rgvTaskET.TASKSTATE = "PickDoing";
								  bool rgv2 = autosan.UpdateRgvTask(rgvTaskET, out string rgvMsg2); 
						break;
						case 3: rgvTaskET.TASKSTATE = "PickFinish";
							      bool rgv3 = autosan.UpdateRgvTask(rgvTaskET, out string rgvMsg3); 
							      //是否需要插入同步表通知类型5 rgv执行中
						break;
						case 4: rgvTaskET.TASKSTATE= "PutDoing";					
									bool rgv4 = autosan.UpdateRgvTask(rgvTaskET, out string rgvMsg4); 
						break;
						case 5: rgvTaskET.TASKSTATE = "PutFinish";
								  bool rgv5 = autosan.UpdateRgvTask(rgvTaskET, out string rgvMsg5); 
						break;
						case 6: rgvTaskET.TASKSTATE = "Cancel";
								  bool rgv6 = autosan.UpdateRgvTask(rgvTaskET, out string rgvMsg6);
						break;
						case 7: rgvTaskET.TASKSTATE = "Error";
								bool rgv7 = autosan.UpdateRgvTask(rgvTaskET, out string rgvMsg7);
						break;
						default: rgvTaskET.TASKSTATE = "null";break;
					}
					//收到json 更新RGV任务状态，并插入到同步表 上报给WMS
					return BuildResponseToRgv(1, true,"success");
				}
				else
				{
					return BuildResponseToRgv(0,false, "任务号不存在");
				}
			}

			catch (Exception ex)
			{
				return BuildResponseToRgv(0,false, ex.Message);
			}
		
		}

	}
}